var list = {

    init: function (option) {
        this.loadData(option);
        // 通用查询功能
        $("#querybtn").click(function () {
            list.query();
        });
        $("#add").click(function () {
            list.add();
        });
        $("#edit").click(function () {
            list.edit();
        });
        $("#del").click(function () {
            list.del();
        });
        $("#detail").click(function () {
            list.detail();
        });

    },
    loadData: function (option) {
        // option.url = BAEPATH + option.url;
        $('#list').datagrid(option);
    },
    query: function () {
        $('#list').datagrid('uncheckAll');
        var obj = $("#query").serializeObject();
        $("#list").datagrid('load', obj);
    },
    add: function () {
        $('#win').window({width: 900, height: 700}).window('open').window("center").window('refresh', addUrl);
    },
    // 打开编辑窗口
    edit: function () {
        var rows = $('#list').datagrid('getChecked');
        if (rows.length != 1) {
            $.messager.alert('编辑提示', '请选择一行数据', 'info');
            $('#list').datagrid('uncheckAll');
            return false;
        }
        try {
            $('#win').window({width: 900, height: 700}).window('open').window("center").window('refresh', editUrl);
            $.get(edit, {id: rows[0].id}, function (msg) {
                if (msg.state == "SUCCESS") {
                    $('#editFrom').form('load', msg.data);
                    //处理图片回显
                    $.each($("#editFrom").find(".bdup"), function (i, v) {
                        var urlName = $(v).attr("urlname");
                        var url = msg.data[urlName];
                        if (url != '' && url != null) {
                            var id = $(v).attr("id");
                            initImg(url, $("#" + id + ""), urlName);
                        }
                    });
                    $.each($("#editFrom").find(".easyui-combobox"), function (i, v) {
                        var name = $(v).attr("textboxname");
                        $(v).combobox('setValue', eval("msg.data." + name));
                    });


                } else {
                    $.messager.show({
                        title: '操作提示',
                        msg: msg.message,
                        timeout: 2000,
                        showType: 'slide'
                    });
                }
            })
        } catch (e) {
        }
    },
    // 通用删除
    del: function () {
        var rows = $('#list').datagrid('getChecked');
        if (rows.length < 1) {
            $.messager.alert('删除提示', '请至少选中选中一行数据', 'info');
            return false;
        }
        $.messager.confirm('确认', '你确定要删除这' + rows.length + '条数据吗?', function (r) {
            if (r) {
                var ids = new Array();
                $.each(rows, function (index, row) {
                    ids.push(row.id);
                });
                $('#list').datagrid('uncheckAll');
                $.post(delUrl, {
                    ids: ids
                }, function (data) {
                    $.messager.show({
                        title: '操作提示',
                        msg: data.message,
                        timeout: 2000,
                        showType: 'slide'
                    });
                    if (data.state == "SUCCESS") {
                        $('#list').datagrid('reload');
                        return true;
                    }
                    return false;
                });
            }
        });

    },
    // 打开查看窗口
    detail: function () {
        var rows = $('#list').datagrid('getChecked');
        if (rows.length != 1) {
            $.messager.alert('查看提示', '请选择一行数据', 'info');
            $('#list').datagrid('uncheckAll');
            return false;
        }
        try {
            $('#win').window({width: 900, height: 700}).window('open').window("center").window('refresh', detailUrl);
            $.get(detail, {id: rows[0].id}, function (msg) {
                if (msg.state == "SUCCESS") {
                    $('#detailFrom').form('load', msg.data);

                    $.each($("#detailFrom").find(".easyui-combobox"), function (i, v) {
                        var name = $(v).attr("textboxname");
                        $(v).combobox('setValue', eval("msg.data." + name));
                    });

                    $('#detailFrom').find("input").each(function () {
                        $(this).attr("disabled", true)
                    });

                    //文本内容预览
                    $.each($("#detailFrom").find(".lbd-text"), function (i, v) {
                        var id = $(v).attr("id");
                        var divshow = $("#" + id + "");
                        divshow.text("");// 清空数据
                        divshow.append(msg.data[id]); // 添加Html内容，不能用Text 或 Val
                    });

                } else {
                    $.messager.show({
                        title: '操作提示',
                        msg: msg.message,
                        timeout: 2000,
                        showType: 'slide'
                    });
                }
            })
        } catch (e) {
        }
    }
}

$("#win").window({
    onClose: function () {
        //窗口关闭后清空窗口内容
        $("#win").empty();
    },
    onLoad: function () {
        $(".save").click(function () {
            if (!$('#saveFrom').form('validate')) {
                return false;
            }
            $.messager.progress();
            $('#saveFrom').form('submit', {
                url: save,
                success: function (data) {
                    $.messager.progress('close');
                    data = $.parseJSON(data);
                    $.messager.show({
                        title: '操作结果',
                        msg: data.message,
                        timeout: 2000,
                        showType: 'slide'
                    });
                    if (data.state == "SUCCESS") {
                        //关闭窗口
                        $("#win").window("close");
                        // 重新加载表格
                        $("#list").datagrid('load');
                    }
                }
            });
        });
        $(".update").click(function () {
            if (!$('#editFrom').form('validate')) {
                return false;
            }
            $.messager.progress();
            $('#editFrom').form('submit', {
                url: edit,
                success: function (data) {
                    $.messager.progress('close');
                    data = $.parseJSON(data);
                    $.messager.show({
                        title: '操作结果',
                        msg: data.message,
                        timeout: 2000,
                        showType: 'slide'
                    });
                    if (data.state == "SUCCESS") {
                        //关闭窗口
                        $("#win").window("close");
                        $('#list').datagrid('uncheckAll');
                        // 重新加载表格
                        $("#list").datagrid('load');
                    }
                }
            });
        });
        $(".upload").click(function () {
            if (!$('#uploadFrom').form('validate')) {
                return false;
            }
            $.messager.progress();
            $('#uploadFrom').form('submit', {
                url: upload,
                success: function (data) {
                    $.messager.progress('close');
                    data = $.parseJSON(data);
                    $.messager.show({
                        title: '操作结果',
                        msg: data.message,
                        timeout: 2000,
                        showType: 'slide'
                    });
                    if (data.state == "SUCCESS") {
                        //关闭窗口
                        $("#win").window("close");
                        $('#list').datagrid('uncheckAll');
                        // 重新加载表格
                        $("#list").datagrid('load');
                    }
                }
            });
        });
    }
});


function parseParam(param, key) {
    var paramStr = "";
    if (param instanceof String || param instanceof Number || param instanceof Boolean) {
        paramStr += "&" + key + "=" + encodeURIComponent(param);
    } else {
        $.each(param, function (i) {
            var k = (key == null || key == 0) ? i : key + ( param instanceof Array ? "[" + i + "]" : "." + i);
            paramStr += '&' + parseParam(this, k);
        });
    }
    return paramStr.substr(1);
};
function initImg(file, $list, paramName) {
    var $li = $(
            '<div id="' + Math.abs(file.__hash) + '" class="file-item thumbnail">' +
            '<img src="' + file + '" width="100px" height="100px" alt="' + file + '" title="' + file + '" >' +
            '<div class="info">' + '<a href="javascript:;" id="remove' + Math.abs(file.__hash) + '"  class="icon-cancel panel-tool-a" style="text-decoration: none">&nbsp; </a>' + file + '</div>' +
            '<input type="hidden" name="' + paramName + '" value="' + file + '">' +
            '</div>'
        ),
        $img = $li.find('img');
    // $list为容器jQuery实例
    $list.append($li);

    $li.find('#remove' + Math.abs(file.__hash)).click(function () {
        console.log($(this).attr("id").replace("remove", "#file"));
        $($(this).attr("id").replace("remove", "#file")).remove()
        $(this).parent().parent().remove();
    });
}
// function formatJson2Url1(param) {
//     var paramStr = "";
//     paramStr += "&" + param + "=" + encodeURIComponent(param);
//     return paramStr.substr(1);
// }
jQuery.fn.extend({
    formatJson2Url: function (key) {
        return parseParam(this, key);
        // var param = this;
        // var paramStr="";
        // $.each(param,function(i){
        //     var k = key==null?i:key+(param instanceof Array?"["+i+"]":"."+i);
        //     paramStr+='&'+ formatJson2Url1(k);
        // });
        // return paramStr.substr(1);
    }
});


/*
 if (id.length == 32) {
 $.getJSON(getbyid + id, function(form) {
 $("#form").form("load", form);
 });
 }*/


